App.controller('employeeGroupController', ['localStore','$rootScope', '$scope', '$state', 'Page', function(localStore,$rootScope, $scope, $state, Page) {

    $scope.grid = {
        options : {
          bAutoWidth : false
        },
        add : function() {
            $scope.employeeGroup = {};
            $scope.modal.show();
        },
        update: function(row) {
            $scope.employeeGroup = row;
            $scope.modal.show();
        },
        reset : function() {
            $scope.groupName = "";
            $scope.grid.query();
        },
        save : function() {
            Page.ajaxPost($scope.employeeGroup.groupId ? 'employeeGroup/update.do' : 'employeeGroup/add.do', $scope.employeeGroup, function() {
                Page.notice("操作成功!");
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        updateMember : function(row, ele) {
            $scope.updateFlag = true;
            $scope.groupMember = {};
            var empName = $(ele).text();
            $(row.groupDetails).each(function(i, member) {
                if (empName == member.empName) {
                    $scope.groupMember = angular.copy(member);
                    return false;
                }
            });
            Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
                $scope.empList = resp.data;
                $scope.groupMember.id = $scope.groupMember.empId;
                $scope.addMemberModal.show();
            });

        },
        addMember : function(row) {
            $scope.groupMember = {};
            $scope.groupMember.groupId = row.groupId;
            Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
                $scope.empList = resp.data;
                $scope.addMemberModal.show();
            });
        },
        column_defs: [{
            target : 3,
            buttons : [
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: 'update'
                },
                {
                    name: '删除',
                    class: 'btn-danger',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('employeeGroup/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };


    $scope.selectable = function(data) {
        var flag = true;
        if(data.id.indexOf("_") != -1) { //选择的部门
            return false;
        }

        var allMembers = localStore.getObject($scope.groupMember.groupId);

        for (var i = 0; i < allMembers.length; i++) {
            var obj = allMembers[i];
            if(obj.empId == data.id) { //选择的人员已经添加到组中
                flag = false;
                break;
            }
        }
        return flag;
    };

    $scope.deleteMember = function(groupMember) {
        Page.confirm("确定要删除吗？", function() {
            Page.ajaxPost('employeeGroup/deleteMember.do', groupMember, function() {
                Page.notice("删除成功!");
                $scope.addMemberModalObj.cancel();
                $scope.grid.query();
            });
        });
    };

    $scope.addMemberModalObj = {
        cancel : function() {
            $scope.memberValidateForm.reset();
            $scope.updateFlag = false;
            $scope.addMemberModal.hide();
        },
        save : function() {
            Page.ajaxPost($scope.updateFlag ? 'employeeGroup/updateMember.do' : 'employeeGroup/saveMember.do',
                $scope.groupMember, function() {
                Page.notice("操作成功!");
                $scope.addMemberModalObj.cancel();
                $scope.grid.query();
            });
        }
    }

    $scope.cancelMemberModal = function() {
        $scope.memberValidateForm.reset();
        $scope.updateFlag = false;
        $scope.addMemberModal.hide();
    }


}]);

App.filter('groupDetails', ['localStore',function(localStore) {
    return function (groupDetails) {
        var html = '';
        if(groupDetails != undefined && groupDetails.length > 0) {
            localStore.setObject(groupDetails[0].groupId, groupDetails);
        }
        $(groupDetails).each(function(index, member) {
            html += '<a class="member-status member-status-' + member.leaderFlag + '" href="javascript:;" ed-click="updateMember">' + member.empName + '</a>'
        });
        html += '<i class="fa fa fa-plus" style="color: #3598dc;cursor: pointer;" ed-click="addMember"></i>';
        return html;
    };
}]);